<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: artlist.lib.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 *
 *内容列表模块
 *@limit 最大显示数
 *@cid 列表ID
 *@titlenum 标题长度
 *@direnum 描述长度
 *@date 时间格式
 *@orderby 排序方式
 *@orderway 降序,升序
 */
class artlist{
	function __construct($C) {
		$date = empty($C['date']) ? 'Y-m-d H:i:s' : $C['date'];
		$limit =  _intC($C, 'limit', 10);
		$cid =  _intC($C, 'cid');
		$whereSql = empty($cid) ? '' : ' WHERE A.cid IN('.$cid.')';
		$titlenum = intC($C, 'titlenum', 40);
		$direnum = intC($C, 'direnum', 180);
		$orderby = 'A.'.(isset($C['orderby']) && in_array($C['orderby'], array('id','time','view','reply')) ? $C['orderby'] : 'id');
		$orderway = isset($C['orderway']) && strtoupper($C['orderway'])=='ASC' ? 'ASC' : 'DESC';

		if(empty($cid)) {
			$_ENV->TV['categoryname'] = '最新文章';
			$_ENV->TV['url'] = 'javascript:;';
		}else{
			 $row = $_ENV->db->fetch_first('SELECT cid,categoryname,dir,parentdir,cate_url FROM `'.TW_DBPRE.'category` WHERE cid IN('.$cid.') LIMIT 1');
			 $_ENV->TV['categoryname'] = $row['categoryname'];
			 $_ENV->TV['url'] = $this->setCatUrl($row);
		}

		$seStr = '';
		$_ENV->G['ishtml'] && $seStr = 'C.dir,C.parentdir,C.cate_url,C.show_url,';
		$sql = 'SELECT '.$seStr.'C.categoryname,A.id,A.cid,A.title,A.direction,A.pic,A.username,A.time,A.view,A.reply FROM `'.TW_DBPRE.'article` A LEFT JOIN `'.TW_DBPRE.'category` C ON C.cid=A.cid'.$whereSql.' ORDER BY '.$orderby.' '.$orderway.' LIMIT '.$limit;
		$query = $_ENV->db->query($sql);
		$data = array();
		while($row = $_ENV->db->fetch_array($query)) {
			$row['date'] = date($date, $row['time']);
			$row['titledesc'] = $row['title'];
			$row['title'] = dstr($row['title'], $titlenum);
			$row['direction'] = dstr($row['direction'], $direnum);
			$row['caturl'] = $_ENV->G['webdir'].$this->setCatUrl($row);
			$row['url'] = $_ENV->G['webdir'].$this->setUrl($row);
			$row['imgsrc'] = empty($row['pic']) ? $_ENV->G['webdir'].TWCMS_STATIC.'img/nopic.gif' : $row['pic'];
			$data[] = $row;
		}
		$this->data = $data;
	}

	function setCatUrl(&$row) {
		if(empty($row['cid'])) return 'javascript:;';
		if($_ENV->G['ishtml']) {	#静态
			return dirname(rep_url($row['cate_url'], $row['parentdir'], $row['dir'], $row['cid'])).'/';
		}elseif($_ENV->G['isfhtml'] && $_ENV->G['custom_cate']) { #分类页伪静态高级
			return $row['dir'].'/';
		}else{	#伪静态/动态
			return (empty($_ENV->G['isfhtml']) ? 'index.php?u=' : '').$_ENV->G['url_cate'].'-'.$row['cid'].$_ENV->G['url_cate_suffix'];
		}
	}

	function setUrl(&$row) {
		if($_ENV->G['ishtml']) {	#静态
			return rep_show_url(rep_url($row['show_url'], $row['parentdir'], $row['dir'], $row['cid']), $row['time'], $row['id']);
		}elseif($_ENV->G['isfhtml'] && $_ENV->G['custom_show']) { #内容页伪静态高级
			return $_ENV->G['url_show'].'-'.$row['id'].$_ENV->G['url_show_suffix'];
		}else{	#伪静态/动态
			return (empty($_ENV->G['isfhtml']) ? 'index.php?u=' : '').$_ENV->G['url_show'].'-'.$row['id'].$_ENV->G['url_show_suffix'];
		}
	}
}